package com.feihao.zhou.fileUpload;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Controller
public class FileUploadTest {

    //测试上传单张图片：
    @ResponseBody
    @RequestMapping("/upload")
    public String testFileUpload(MultipartFile file, HttpServletRequest request) throws IOException {
        file.transferTo(new File(request.getServletContext().getRealPath("img"), UUID.randomUUID().toString()+file.getOriginalFilename()));
        return "上传ok";
    }

    //测试上传多张：

   /* @RequestMapping("/upload1")
    @ResponseBody
    public String upload1(MultipartFile[] file12,HttpServletRequest request) throws IOException {
        if (file12!=null){
            for (MultipartFile file : file12) {
                file.transferTo(new File(request.getServletContext().getRealPath("/img"),UUID.randomUUID().toString()+file.getOriginalFilename()));
            }
        }
        return "上传ok!";
    }*/

    @RequestMapping("/upload1")
    @ResponseBody
    public String upload1(MultipartFile[] file1, HttpServletRequest request) throws IOException {
        if (file1!=null){
            for (MultipartFile file : file1) {
                file.transferTo(new File(request.getServletContext().getRealPath("/img") , UUID.randomUUID().toString()+file.getOriginalFilename()));
            }
        }
        return "上传ok";
    }
}
